package com.laoxu.test.rbacdemo.controller;


import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.laoxu.test.rbacdemo.common.Result;
import com.laoxu.test.rbacdemo.exception.AppException;
import com.laoxu.test.rbacdemo.model.SysMenu;
import com.laoxu.test.rbacdemo.model.VueRouter;
import com.laoxu.test.rbacdemo.service.SysMenuService;
import com.laoxu.test.rbacdemo.service.UserManager;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  菜单控制器
 * </p>
 *
 * @author laoxu
 * @since 2020-08-09
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/menu")
public class SysMenuController extends BaseController{
    private String message;

    @Autowired
    private UserManager userManager;
    @Autowired
    private SysMenuService menuService;

    @GetMapping("/getUserRouters")
    public ArrayList<VueRouter<SysMenu>> getUserRouters(@NotBlank(message = "{required}") @RequestParam String username) {
        return this.userManager.getUserRouters(username);
    }

    @GetMapping("/menuList")
    @RequiresPermissions("menu:view")
    public Map<String, Object> menuList(SysMenu menu) {
        return this.menuService.findMenus(menu);
    }

    @PostMapping("/add")
    @RequiresPermissions("menu:add")
    public Result addMenu(@RequestBody @Valid SysMenu menu) throws AppException {
        try {
            this.menuService.createMenu(menu);
            return new Result().ok("新增菜单/按钮成功");
        } catch (Exception e) {
            message = "新增菜单/按钮失败";
            log.error(message, e);
            throw new AppException(message);
        }
    }

    @PostMapping("/remove")
    @RequiresPermissions("menu:delete")
    public Result deleteMenus(@NotBlank(message = "{required}") @RequestParam String menuIds) throws AppException {
        try {
            String[] ids = menuIds.split(StringPool.COMMA);
            this.menuService.deleteMeuns(ids);
            return new Result().ok("删除菜单/按钮成功");
        } catch (Exception e) {
            message = "删除菜单/按钮失败";
            log.error(message, e);
            throw new AppException(message);
        }
    }

    @PostMapping("/modify")
    @RequiresPermissions("menu:update")
    public Result updateMenu(@RequestBody @Valid SysMenu menu) throws AppException {
        try {
            this.menuService.updateMenu(menu);
            return new Result().ok("success");
        } catch (Exception e) {
            message = "修改菜单/按钮失败";
            log.error(message, e);
            throw new AppException(message);
        }
    }

    @PostMapping("excel")
    @RequiresPermissions("menu:export")
    public void export(@RequestBody SysMenu menu, HttpServletResponse response) throws AppException {
        try {
            List<SysMenu> menus = this.menuService.findMenuList(menu);
            ExcelKit.$Export(SysMenu.class, response).downXlsx(menus, false);
        } catch (Exception e) {
            message = "导出Excel失败";
            log.error(message, e);
            throw new AppException(message);
        }
    }
}
